package top.zackyoung.bbs.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: ZackYoung
 * @time: 2020/5/28
 */
public class CharsetFilter implements Filter {


    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterSetFilter初始化成功");
        //初始化路径

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        System.out.println(req.getCharacterEncoding());
        //防止乱码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //打印一下过滤的访问路径
        String path = req.getServletPath();
        System.out.println("CharacterSetFilter:" + path);
        System.out.println(resp.getCharacterEncoding());

        //执行下一个过滤器
        filterChain.doFilter(req, resp);
    }

    public void destroy() {
        System.out.println("CharacterSetFilter销毁");
    }

}
